#!/usr/bin/env python
# -*-*- encoding: utf-8 -*-*-
#
#
# Copyright 2007-2008 Håvard Gulldahl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A desktop client for PicasaWeb services."""

__author__ = u'havard@gulldahl.no (Håvard Gulldahl)'


import sys
from PyQt4 import QtGui, QtCore

class PicasaPicWidget(QtGui.QLabel):
  entry = None
  rawpix = None

  def dragObject(self):
    print "dragobject"
    mimeData = QtCore.QMimeData()
    #mimeData.setData(
    drag = QtGui.QDrag(self)
    drag.setMimeData(mimeData)
    drag.setHotSpot(event.pos())
    drag.start(QtCore.Qt.CopyAction)

  def mousePressEvent(self, event):
    print "mousePressEvent: %s (event type: %d)" % (type(event), event.type())
    if not self.entry or not self.rawpix: #image not set
      return QtGui.QLabel.mousePressEvent(self, event)
    pix = self.rawpix #pixmap()
    event.accept()
    imageData = QtGui.QImage(pix.toImage())
    mimeData = QtCore.QMimeData()
    mimeData.setImageData(QtCore.QVariant(imageData))
    url = self.entry.GetMediaURL()
    if url is None:
      try: url = self.entry.content.src
      except: pass
    if url is None:
      try: url = self.entry.media.content[0].url
      except:
        return QtGui.QLabel.mousePressEvent(self, event)
    print self.entry.GetSelfLink().href
    mimeData.setUrls([ QtCore.QUrl(url), ])
    drag = QtGui.QDrag(self)
    drag.setPixmap(pix)
    drag.setMimeData(mimeData)
    drag.setHotSpot(event.pos())
    drag.start(QtCore.Qt.CopyAction)
    return QtGui.QLabel.mousePressEvent(self, event)

  #def resizeEvent(self, event):
    #print "resizeEvent: %s (event type: %d)" % (type(event), event.type())
    #if not self.rawpix: #image not set
      #return QtGui.QLabel.resizeEvent(self, event)
    ##self.setScaledPixmap()
    ##self.setPixmap(self.rawpix)
    #self.update()

    #return QtGui.QLabel.resizeEvent(self, event)

  #def paintEvent(self, event):
    #print "paintEvent: %s (event type: %d)" % (type(event), event.type())
    ##if not self.rawpix: #image not set
      ##return QtGui.QLabel.paintEvent(self, event)
    ###self.setScaledPixmap()
    ###self.setPixmap(self.rawpix)

    #return QtGui.QLabel.paintEvent(self, event)

  def setPixmap(self, pixmap): # set a proportionally correctly scaled pixmap
    #self.pixmap().fill(QtGui.QColor("black"))
    size = pixmap.size()
    size.scale(self.size(), QtCore.Qt.KeepAspectRatio)
    QtGui.QLabel.setPixmap(self, pixmap.scaled(size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))


